package com.sias;

import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Arrays;

/**
 * @author 吴文杰
 * @version 1.0
 * @since 2023/6/7 10:59
 */
@Configuration
@ComponentScan
@PropertySource("classpath:application.yml")
@EnableConfigurationProperties({WebMvcProperties.class, ServerProperties.class})
public class WebConfig {

  @Bean
  public TomcatServletWebServerFactory tomcatServletWebServerFactory(){
    return new TomcatServletWebServerFactory();
  }

  @Bean
  public DispatcherServlet dispatcherServlet(){
    return new DispatcherServlet();
  }

  @Bean
  public DispatcherServletRegistrationBean dispatcherServletRegistrationBean(
          DispatcherServlet dispatcherServlet,WebMvcProperties webMvcProperties){
    DispatcherServletRegistrationBean registrationBean =
            new DispatcherServletRegistrationBean(dispatcherServlet, "/");
    registrationBean.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
    return registrationBean;
  }

  @Bean
  public RequestMappingHandlerMapping requestMappingHandlerMapping(){
    return new RequestMappingHandlerMapping();
  }

  @Bean
  public RequestMappingHandlerAdapter requestMappingHandlerAdapter(){
    MyHandlerMethodArgumentResolver resolver = new MyHandlerMethodArgumentResolver();
    RequestMappingHandlerAdapter handlerAdapter = new RequestMappingHandlerAdapter();
    handlerAdapter.setArgumentResolvers(Arrays.asList(resolver));
    return handlerAdapter;
  }

}
